//
//  AnimationHelper.swift
//  TooToo
//
//  Created by liuning on 15/12/23.
//  Copyright © 2015年 MoHao. All rights reserved.
//

import Foundation

typealias completed = () -> Void

class AnimationHelper : NSObject {
    
    /* 大小变化 */
    
    @objc class func addBounceForView (_ view: UIView, scale: CGFloat, duration: CGFloat, completedBlock: completed!){
        
        //usingSpringWithDamping 阻尼值
        //initialSpringVelocity 弹簧初始速率
        
        UIView.animate(withDuration: TimeInterval(duration/2.0), animations: {
            
            view.transform = CGAffineTransform(scaleX: scale, y: scale)
            
        }) { (compe) in
            
            UIView.animate(withDuration: TimeInterval(duration/2.0), animations: {
                
                view.transform = CGAffineTransform(scaleX: 1, y: 1)
                
            }) { (compe) in
                
                if (completedBlock) != nil{
                    completedBlock()
                }
            }
        }
        
        
    }
    
    
    /* 弹跳动画 */

    @objc class func addBounceForView (_ view: UIView, fromValue: CGFloat, toValue: CGFloat){
        
        //usingSpringWithDamping 阻尼值
        //initialSpringVelocity 弹簧初始速率
        
        view.transform = CGAffineTransform(scaleX: fromValue, y: fromValue)
        
        UIView.animate(withDuration: 0.7, delay:0.1,usingSpringWithDamping:0.3,initialSpringVelocity:0,options:UIViewAnimationOptions(),animations: { () -> Void in
            
            view.transform = CGAffineTransform(scaleX: toValue, y: toValue)
            
            }) { (compe) -> Void in
                //do something
        }
    }
    
    /* 旋转移动动画 */
    
    @objc class func addRotateMoveForView (_ view: UIView, fromRect: CGRect,toRect: CGRect, completedBlock: completed!) {
        
        //执行动画前，禁止一切操作
        let keyWin:UIWindow! = UIApplication.shared.keyWindow
        keyWin.isUserInteractionEnabled = false
        
        
        
        let copiedView = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: view)) as! UIView
        keyWin.addSubview(copiedView)
        copiedView.frame = fromRect
        
        /*
        let duration = 0.6
        
        //1: 旋转动画
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.fromValue = 0
        rotationAnimation.toValue = M_PI * 2 * 3
        rotationAnimation.duration = duration
        rotationAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        //执行动画
        copiedView.layer.addAnimation(rotationAnimation, forKey: rotationAnimation.keyPath)
        */
        
        //1：旋转动画
        let rotation = JNWSpringAnimation(keyPath: "transform.rotation.z")
        //设置阻尼
        rotation?.damping = 0
        //设置弹性系数，就是用多大的力按弹簧
        rotation?.stiffness = 5.5
        //质量
        rotation?.mass = 0.07
        rotation?.fromValue = 0
        //转几圈
        rotation?.toValue = M_PI * 2 * 5
        //执行动画
        copiedView.layer.add(rotation!, forKey: rotation?.keyPath)
        
        //2: 大小和位置变化
        UIView.animate(withDuration: (rotation?.duration)!, delay:0.0,usingSpringWithDamping:2,initialSpringVelocity:0,options:UIViewAnimationOptions(),animations: { () -> Void in
            
            copiedView.frame = toRect
            
            }) { (compe) -> Void in
                
                //动画结束后执行
                copiedView.removeFromSuperview()
                keyWin.isUserInteractionEnabled = true
                
                if (completedBlock) != nil{
                    completedBlock()
                }
        }
        
        
        
        
    }
    
    
    /* 渐变动画 */
    
    @objc class func addAlphaChangeForView (_ view: UIView, fromFloat: CGFloat, toFloat: CGFloat ,duration: TimeInterval,completedBlock: completed!){
    
        view.alpha = fromFloat;
        
        UIView.animate(withDuration: duration, animations: { () -> Void in
            
            view.alpha = toFloat;
            
            }, completion: { (completed) -> Void in
                
                if (completedBlock) != nil{
                    completedBlock()
                }
        }) 
    }
    
    /* 大小变化动画 */
    
    @objc class func addScaleChangeForView (_ view: UIView, toValue: CGFloat ,duration: TimeInterval,completedBlock: completed!){
        
        
        UIView.animate(withDuration: duration, animations: { () -> Void in
            
            view.transform = CGAffineTransform(scaleX: toValue, y: toValue);
            
            }, completion: { (completed) -> Void in
                
                if (completedBlock) != nil{
                    completedBlock()
                }
        }) 
    }
    
    /* 位置变化 */
    
    @objc class func addPositionChangeForView (_ view: UIView, fromOrigin: CGPoint, toOrigin: CGPoint ,duration: TimeInterval,completedBlock: completed!){
        
        var frame = view.frame
        frame.origin = fromOrigin
        view.frame = frame
        
        UIView.animate(withDuration: duration, animations: { () -> Void in
            
            frame.origin = toOrigin
            view.frame = frame
            
            }, completion: { (completed) -> Void in
                
                if (completedBlock) != nil{
                    completedBlock()
                }
        }) 
    }
    
    
    /* 3D旋转
    
    anchorPoint:锚点 默认是（0.5，0.5）
    position:中心点的位置，配合锚点使用
    m34:透视度（负数代表往里透）
    angel:偏转调度
    XYZ:旋转轴（取值范围0-1）
    
    */
    @objc class func add3DRotationForView (_ view: UIView, anchorPoint: CGPoint, position: CGPoint, m34: CGFloat, angel: CGFloat, x: CGFloat, y: CGFloat, z: CGFloat, duration: TimeInterval, completedBlock: completed!){
    
        view.isUserInteractionEnabled = false
        
        let layer = view.layer
        layer.anchorPoint = anchorPoint
        layer.position = position
        
        var rotationTransform = CATransform3DIdentity
        rotationTransform.m34 = m34
        
        UIView.animate(withDuration: duration, animations: { () -> Void in
            
            layer.transform = CATransform3DRotate(rotationTransform, angel, x, y, z)
            
            }, completion: { (completed) -> Void in
                
                if (completedBlock) != nil{
                    completedBlock()
                }
        }) 
    
    }
   
    @objc class func add3DGoBackForView (_ view:UIView){
        
        view.isUserInteractionEnabled = false
        
        view.superview?.backgroundColor = UIColor.black
        
        let anchorPoint = CGPoint(x: 0.5, y: 1)
        let position = CGPoint(x: view.bounds.size.width/2.0, y: view.bounds.size.height)
        let angel = CGFloat(5.0 * M_PI / 180.0)
        
        self.add3DRotationForView(view, anchorPoint: anchorPoint, position: position, m34: -0.001, angel: angel, x: 1, y: 0, z: 0, duration: 0.3) { () -> Void in
            
            self.addScaleChangeForView(view, toValue: 0.95, duration: 0.3, completedBlock: { () -> Void in
                view.isUserInteractionEnabled = true
            });
        }
    }
    
    @objc class func add3DGoFrontForView (_ view:UIView){
        
        view.isUserInteractionEnabled = false
        
        let anchorPoint = CGPoint(x: 0.5, y: 1)
        let position = CGPoint(x: view.bounds.size.width/2.0, y: view.bounds.size.height)
        let angel = CGFloat(-5.0 * M_PI / 180.0)
        
        self.add3DRotationForView(view, anchorPoint: anchorPoint, position: position, m34: 0.001, angel: angel, x: 1, y: 0, z: 0, duration: 0.3) { () -> Void in
            
            self.addScaleChangeForView(view, toValue: 1, duration: 0.3, completedBlock: { () -> Void in
                view.isUserInteractionEnabled = true
                view.superview?.backgroundColor = UIColor.white
                //锚点和位置复原
                view.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
                view.layer.position = CGPoint(x: view.bounds.size.width/2.0, y: view.bounds.size.height/2.0)
                
            });
        }
    }
    
}
